<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_Controller {

	
	 function __construct()
    {
        parent::__construct();
        if(!$this->session->userdata('logged_in'))
        {
        	$this->session->set_flashdata('message', 'login');
        	redirect('login');
        }	
    }

	 function index()
	{
		$data = $this->session->userdata('logged_in');
		$this->load->view('layouts/header_view', $data);
		$this->load->view('admin_page', $data);
	}

	function upload(){

		$this->load->model('doc_types_model');
		$this->load->model('partners_model');
		$data['doc_types_query'] = $this->doc_types_model->get_types();
		$data['partners_query'] = $this->partners_model->get_partners();

		$this->load->view('layouts/header_view');
		$this->load->view('upload_view',$data);
	}

	function test(){
		$p = $this->input->post();
		$t = gettype($p['created_on']);
	 	echo $t;
	

		//print_r($query->result()[0]->version);
		// foreach ($query->result() as $q) {
		// 	print_r($q->path);
		// 	echo '<br/>';
		// }
	}

	function do_upload(){
		
		
		$p = $this->input->post();

		
		$date = date('d-m-y-h-i-s');

		$new_file_name = $p['partner'] . ('-') . $p['doc_type'] . ('-') . $p['created_on'];

		$this->db->like('path',$new_file_name);
		$this->db->select_max('version');
		$query = $this->db->get('files');

		if(isset($query->result()[0]->version)){
			$new_version = $query->result()[0]->version + 1;
		}

		else{
			$new_version = 1;
		}

		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'gif|jpg|docx|pdf';
		$config['max_size']	= '500';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';


		$config['file_name'] =  $new_file_name . ('-') . 'v' . $new_version;

		

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload())
		{
			$this->session->set_flashdata('message', 'upload_error');
			redirect('site/upload');
		}
		else
		{
			$data = array('upload_data' => $this->upload->data());

			$this->load->model('files_model');
			$info= array(
				'path' => $config['file_name'] . $data['upload_data']['file_ext'],
				'doc_type' => $p['doc_type'],
				'partner' => $p['partner'],
				'comment' => $p['comment'],
				'created_on' => $p['created_on'],
				'version' => $new_version
			);	

			
			$this->files_model->insert_db($info);
			$this->session->set_flashdata('message', 'upload_success');
			redirect('site/upload'); 

		} 
	}

	 function files(){

		
		$p = $this->input->post();
		$doc_type = $p['doc_type'];
		$partner = $p['partner'];
		$order = $p['order'];
		$created_from = $p['created_from'];
		$created_to = $p['created_to'];

		$this->load->model('files_model');
		$this->load->model('doc_types_model');
		$this->load->model('partners_model');

		$data['partners_query'] = $this->partners_model->get_partners();
		$data['doc_types_query'] = $this->doc_types_model->get_types();
		$data['query'] = $this->files_model->get_files($doc_type,$partner,$order,$created_from,$created_to);

		$data['order'] = $order;
		$data['doc_type'] = $doc_type;
		$data['partner'] = $partner;

		$data['created_from'] = $created_from;
		$data['created_to'] = $created_to;

		$this->load->view('layouts/header_view');
		$this->load->view('files_view',$data);
		

		/*  

		// sa pagination

		$this->load->library('pagination');
		$this->load->library('table');
	

		$this->db->select('path, doc_type');	


		$p = $this->input->post();
		$doc_type = $p['doc_type'];
		$order = $p['order'];

		$data['base_url'] = '/cotee/site/files/';

		$data['total_rows'] = $this->db->get('files')->num_rows();
		

		$data['per_page'] = 20;
		$data['num_links'] = 10;

		//$data['records'] = $this->db->select('path, doc_type')->get('files',$data['per_page'],$this->uri->segment(3));

		$this->load->model('files_model');

		$data['records'] = $this->files_model->get_pag_files($data['per_page'],$doc_type,$order);

		$data['order'] = $order;
		$data['doc_type'] = $doc_type;	

		$this->pagination->initialize($data);   


		$this->load->view('layouts/header_view');
		$this->load->view('files_view',$data);	
*/

	}

	function delete($id){

		
		$this->load->helper("file");
		
		$this->db->select('path');
		$this->db->from('files');
		$this->db->where('id',$id);

		$path = $this->db->get()->row()->path;

		// na serveru 10.1.1.63 promijeniti $full_path = "/var/www/html/cotee/uploads/" . $path; 

		$full_path = "C:/wamp/www/cotee/uploads/" . $path;

		$this->db->delete('files',array('id'=>$id));
		unlink($full_path);
		
		echo '<script>alert("Dokument je uspjesno obrisan!");</script>';
        
		redirect('site/files','refresh');
	}


	function update($id){

		$this->load->model('doc_types_model');
		$this->load->model('partners_model');
		$data['partners_query'] = $this->partners_model->get_partners();
		$data['doc_types_query'] = $this->doc_types_model->get_types();

		$this->db->select('*');
		$this->db->from('files');
		$this->db->where('id',$id);

		$query = $this->db->get()->result();

		
	
		
		$data['doc_type'] = $query[0]->doc_type;
		$data['partner'] = $query[0]->partner;
		$data['path'] = $query[0]->path;
		$data['comment'] = $query[0]->comment;
		$data['created_on'] = $query[0]->created_on;
		$data['id'] = $id;

		$this->load->view('layouts/header_view');
		$this->load->view('update_view',$data);

	}

	function do_update($id){
		
		
		$p = $this->input->post();

		
		
		$this->db->select('*');
		$this->db->from('files');
		$this->db->where('id',$id);

		$query = $this->db->get()->result();
		$path = $query[0]->path;
		$version = $query[0]->version;
		
		$info = array(
			'comment' => $p['comment'],
			'created_on' => $p['created_on'],
			'partner' => $p['partner'],
			'doc_type' => $p['doc_type'],
			'version' => $version
		);

		$new_file_name = $p['partner'] . ('-') . $p['doc_type'] . ('-') . $p['created_on'] . ('-') . 'v' . $version; 

		// na serveru 10.1.1.63 promijeniti $old_path i $new_path u "/var/www/html/cotee/uploads/" . $path; 

		if (empty($_FILES['userfile']['name'])) {
			$old_path = "C:/wamp/www/cotee/uploads/" . $path;
			
			$ext = pathinfo($old_path, PATHINFO_EXTENSION);

			$new_path = "C:/wamp/www/cotee/uploads/" . $new_file_name . '.' . $ext;
			rename($old_path,$new_path);
			$info['path'] = $new_file_name . '.' . $ext;
			
		}

		else{
			
			$old_path = "C:/wamp/www/cotee/uploads/" . $path;
							
			unlink($old_path);

			$config['upload_path'] = './uploads/';
			$config['allowed_types'] = 'gif|jpg|docx|pdf';
			$config['max_size']	= '500';
			$config['max_width']  = '1024';
			$config['max_height']  = '768';

		
			$config['file_name'] = $new_file_name;
			

			$this->load->library('upload', $config);


			if ( ! $this->upload->do_upload())
			{
				$error = array('error' => $this->upload->display_errors());

				$this->load->view('upload_form', $error);
			}

			else
			{
				$data = array('upload_data' => $this->upload->data());

				$info['path'] = $config['file_name'] . $data['upload_data']['file_ext'];				
			}

			
		}

		$this->db->flush_cache();

		$this->db->select('*');
		$this->db->from('files');
		$this->db->where('id',$id);	
		$this->db->update('files',$info);


		echo '<script>alert("Dokument je uspjesno izmijenjen!");</script>';
        
		redirect('site/files','refresh'); 

	}

}
